<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/22
 * Time: 15:30
 */

namespace App\Api\Admin\Controllers;


use App\Exceptions\ApiException;
use App\Models\User;
use Illuminate\Http\Request;
use App\Models\Usertags;
use App\Models\Tagsname;
use App\Models\Admin\Log;
class TagsController extends CommonController
{
    public function __construct()
    {
        parent::__construct();

    }
    /**
     * 用户的标签列表
     */
    public function index(Request $request)
    {
        $page = intval($request->input('page')) ?: 1;
        $page = $page - 1;
        $pageSize = 10;
        $name = $request->input('name');
        $where = [];
        if ($name) {
            $where[] = ['tags_name', 'like', '%' . $name . '%'];
        }
        $data = Tagsname::orderBy('id', 'ASC')->get();
        return json_success('ok', $data);
    }

    /**
     * 添加栏目标签
     */
    public function AddTags(Request $request)
    {
        $name = IT('name', true, '请添加栏目名称');
        $insert_data = [
            'tags_name' => $name,
            'create_at' => time2date()
        ];
        $inspect_name = Tagsname::where('tags_name', '=', $name)->first();
        if ($inspect_name) {
            EE('该栏目名称已存在');
        }
        $insert = Tagsname::insert($insert_data);
        if ($insert) {
            (new Log)->add($this->user, '添加了标签栏目为--' . $name);
            return json_success('ok');
        } else {
            return json_error('添加失败');
        }
    }
    /**
     * 标签栏目的删除
     */
    public function DelTags(Request $request)
    {
        $name = IT('tags_name', true, '请添加栏目名称');
        $tags_name =Tagsname::where('tags_name', '=',$name)->first();
         if(!$tags_name){
           EE('没有这个栏目');
         }
        $delete = Tagsname::where('tags_name', '=',$name)->delete();
        if ($delete) {
            (new Log)->add($this->user, '删除了' . '"' . $name . '"' . '标签栏目');
            return json_success('ok');
        } else{
            return json_error('删除失败');
        }
    }

}